package de.tlabs.ahmad.trigger_server;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import com.google.code.microlog4android.Logger;
import com.google.code.microlog4android.LoggerFactory;

public class TriggerServer implements Runnable {
	
	public static PrintWriter output;
	static Socket s=null;
	static ServerSocket ss;
	
	public static PrintWriter getOutputStream()
	{
		return output;
	}
	public static void StopTriggerServer() throws IOException
	{
		if(ss.isClosed())
			output.flush();
			output.close();
			s.close();	
	}
	
	private static final Logger logger = LoggerFactory.getLogger();

		public void run()
		{
			Boolean end = false;
			//ss = null;
			try {
				ss = new ServerSocket(3233);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
				logger.info("Running TriggerServer");
			    while(!end)
			    {
			    	//Server is waiting for client here, if needed
					try {
						s = ss.accept();
						
					} catch (IOException e) {
						e.printStackTrace();
					}
			       // BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
			        try {
						output = new PrintWriter(s.getOutputStream(),true);
						/*
						for(int i =0; i<10; i++)
						{
							logger.info("Writing to client");
							output.print(0x60);
							output.flush();
						}*/
					} catch (IOException e) {
						e.printStackTrace();
					}
			    }
			    try {
					output.flush();
					output.close();
			    	ss.close();
				} catch (IOException e) {
					e.printStackTrace();
				}		        
		}
		
}
